package com.cloudkinto.controller.product;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.product.ProductApproveBo;
import com.cloudkinto.bo.product.ProductQueryBo;
import com.cloudkinto.bo.product.ProductUpdateBo;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.product.ProductService;
import com.cloudkinto.vo.common.CommonBatchOperationVo;
import com.cloudkinto.vo.product.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * 产品表 前端控制器
 * </p>
 *
 * @author long
 * @since 2024-10-29
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "omp产品")
@RequestMapping("/api/admin/omp/product")
public class ProductOmpController extends BaseController {

    private final ProductService productService;

    @UserSessionRequired
    @PutMapping
    @ApiOperation(value = "产品表修改", notes = "产品表修改")
    public SingleResult<String> update(@RequestBody @Validated ProductUpdateBo bo) {
        productService.update(bo, getCurrentUserId(), getUserName(), getTenantId());
        return SingleResult.success("修改成功");
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "产品表根据id查看详情", notes = "产品表根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", productService.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "产品表查询初始化", notes = "产品表查询初始化")
    public SingleResult pageInit() {
        return productService.pageInit(getCurrentUserId(), getTenantId(), "omp");
    }

    @UserSessionRequired
    @PostMapping("/listPage")
    @ApiOperation(value = "产品表查询（分页）", notes = "产品表查询（分页）")
    @ApiResponses(@ApiResponse(code = 200, message = "请求成功", response = ProductVo.class))
    public PageResult listPage(@RequestBody ProductQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("omp");
        return productService.getListPage(bo);
    }

    @UserSessionRequired
    @PostMapping("/statusStatistics")
    @ApiOperation(value = "产品表状态统计", notes = "产品表状态统计")
    public SingleResult statusStatistics(@RequestBody @Validated ProductQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("omp");
        bo.setApproveStatus(null);
        return productService.statusStatistics(bo);
    }

    @UserSessionRequired
    @PostMapping("/approve")
    @ApiOperation(value = "产品审核", notes = "产品审核")
    public SingleResult approve(@RequestBody ProductApproveBo bo) {
        productService.approve(bo, getCurrentUserId(), getUserName());
        return SingleResult.success("审核成功");
    }

    @UserSessionRequired
    @GetMapping("/getImportUpdateProductTemplate")
    @ApiOperation(value = "获取导入编辑产品模板")
    public void getOmpImportUpdateProductTemplate(HttpServletResponse response) throws IOException {
        productService.getOmpImportUpdateProductTemplate(response, getTenantId(), getCurrentUserId());
    }

    @UserSessionRequired
    @PutMapping("/importUpdateProduct")
    @ApiOperation(value = "导入编辑产品", notes = "导入编辑产品")
    @ApiResponses(@ApiResponse(code = 200, message = "操作成功", response = CommonBatchOperationVo.class))
    public SingleResult importUpdateProduct(MultipartFile file) {
        return SingleResult.success(productService.importUpdateProductOmp(file, getCompanyId(), getCurrentUserId(), getUserName(), getTenantId()));
    }

    @UserSessionRequired
    @PostMapping("/export")
    @ApiOperation(value = "导出产品", notes = "导出产品")
    public void export(@RequestBody ProductQueryBo bo, HttpServletResponse response) throws Exception {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("omp");
        productService.export(bo, response);
    }
}

